<?php
namespace Admin\Controller;

use Admin\Controller\CommonController;

class AreaController extends CommonController
{

    public function index(){
        $this->display();
    }

    public function arealist(){
        $field = 'id,area,areacode,orderid,is_business';
        if(empty($_POST['id'])){
            $map['areacode'] = array(
                array(
                    'like',
                    '%0000'
                ),
                array(
                    'neq',
                    '100000'
                ),
                'AND');

            $res = M('area')
                ->where($map)
                ->field($field)
                ->select();
            foreach ($res as $ko => $vo) {
                if(!in_array($vo['areacode'],array('710000','810000','820000'))){
                    $res[$ko]['state'] = "closed";
                }
            }
        }else{
            $areacode = $_POST['id'];
            $zero_num = zero_end_num($areacode);
            if ($zero_num == 4) {
                $prefix = substr($areacode, 0, 2);
                if (in_array($prefix, array(11, 12, 31, 50))){
                    $where['areacode'] = array(
                        array(
                            'like',
                            $prefix.'%'
                        ),
                        array(
                            'neq',
                            $areacode),
                        'AND');
                    $res = M('area')
                        ->where($where)
                        ->select();
                }else{
                    $where['areacode'] = array(
                        array(
                            'like',
                            $prefix.'%'.'00'
                        ),
                        array(
                            'neq',
                            $areacode),
                        'AND');
                    $res = M('area')
                        ->where($where)
                        ->select();
                    foreach ($res as $ko=>$vo){
                        $res[$ko]['state'] = "closed";
                    }
                }
            }elseif ($zero_num == 3 || $zero_num == 2) {
                $prefix = substr($areacode, 0, 4);
                $where['areacode'] = array(
                    array(
                        'like',
                        $prefix . '%'
                    ),
                    array(
                        'neq',
                        $areacode),
                    'AND');
                $res = M('area')
                    ->where($where)
                    ->select();
            }
        }
        $this->ajaxReturn($res);
    }

    public function change_business()
    {
        parse_str($_POST['jsonCasData'], $formdata);
        $type = $formdata['action'];
        if ($type == 'update') {
            $where['id'] = $formdata['id'];
            $data['is_business'] = $formdata['is_business'];
            $option = M('area')->where($where)->save($data);
            if ($option !== false) {
                $data = [
                    'status' => 1,
                    'msg' => '数据修改成功'
                ];
            } else {
                $data = [
                    'status' => 0,
                    'msg' => '数据修改失败'
                ];
            }
        }
        $this->ajaxReturn($data);
    }
}
